<?php
/**
 * Công cụ phát triển mã nguồn
 *
 * Một sản phẩm tự sinh mã tự động
 *
 * @version $Rev:$
 * @license New BSD
 */
class Generate_IndexController extends Zend_Controller_Action{
		
	function init(){
		$this->Params = $this->getRequest()->getParams();
        System_Template::using('getit','Admin');
		parent::init();
	}
    /**
    * Màn hình tạo mã nguồn
    */
	function indexAction(){        
		$array = config::getAuth();
        $arrayModule = array ();
        while ($fruit_name = current($array)) {
            $arrayModule[] = key($array);
            next($array);
        }
        $table = new System_Table_Roles();
        $select = $table->select();
        $select->from($table,array('NAME','CODE'));
        $list = $table->fetchAll($select);
        
        if(isset($this->Params['moduleInfo']) && $this->Params['moduleInfo'] != ''){
            $moduleInfo = config::getModule($this->Params['moduleInfo']);
            
            $this->view->moduleInfo = $moduleInfo ;
            $this->view->moduleSelected = $this->Params['moduleInfo'];
        }
        
        $this->view->roles = $list;
        $this->view->arrModule = $arrayModule ;
    }
    
    /**
    * Thực hiện tạo mã nguồn
    */
    function saveAction(){    
        $isAuth      = $this->Params['isAuth'];
        //$alowRoles      = $this->Params['alowRoles'];
        $controllerList = $this->Params['controllerList'];
        $exceptionPath  = $this->Params['exceptionPath'];
        //$deny           = $this->Params['deny'];
        $moduleName     = $this->Params['moduleName'];
        $moduleCreate   = strtolower($this->Params['moduleCreate']);
        $strController  = implode(",", $controllerList);
        //$alowRoles      = implode(",", $alowRoles);
        
        $data = array (
            'name' => $moduleName,
            'controllers' => strtolower($strController),
            'allow' => '',
            'gotourlandexit' => $exceptionPath,
            'isauth' => "0",
            'deny' => ''
        );
        
        $model = new Generate_Model_Generate();
        if(!isset($this->Params['isUpdate'])){
            $model->createModule($this->Params);
        }else{
            $moduleInfo = config::getModule($this->Params['moduleInfo']);
            $data['allow'] = $moduleInfo['allow'];
            $data['deny'] = $moduleInfo['deny'];
        }
        config::set('auth.ini',$moduleCreate,$data);
        $model->createModel($this->Params);
        $model->createController($this->Params);
        $model->createView($this->Params);
        $this->_redirect('/generate/index');
    }
    
    /**
    * Màn hình danh sách các layout
    */
    function getlistlayoutAction(){
        $layoutPath = "../application/templates/".$this->Params['templateName']."/temp1";
        if ($handle = opendir($layoutPath)) {
            while (false !== ($entry = readdir($handle))) {
                $ext = explode(".",$entry);
                if($entry != '.' && $entry != '..' && $entry != '.svn' && $ext[1] == 'phtml'){
                    $layout = explode(".",$entry);
                    echo "<option value='".$layout[0]."'>$entry</option>";
                }
            }
            closedir($handle);
        }
        exit;
    }
	function testAction(){
        $controller_path = APPLICATION_PATH."/modules/testmodule";
        mkdir($controller_path, 0700);
        $file = new Zend_CodeGenerator_Php_File(array(
            'classes' => array(
                new Zend_CodeGenerator_Php_Class(array(
                    'name'    => 'World',
                    'methods' => array(
                        new Zend_CodeGenerator_Php_Method(array(
                            'name' => 'hello',
                            'body' => 'echo \'Hello world!\';',
                        )),
                    ),
                )),
            )
        ));
         
        // Configuring after instantiation
        $docblock = new Zend_CodeGenerator_Php_Docblock();
        $docblock->setShortDescription('OK cái này tui set');
        $docblock->setLongDescription('OK cái này là longDescription');
        $tag = array(
            array(
                'name'        => 'version',
                'description' => '$Rev:$',
            ),
            array(
                'name'        => 'license',
                'description' => 'New BSD',
            )
        );
        $docblock->setTags($tag);
        $method = new Zend_CodeGenerator_Php_Method();
        $method->setName('hello')
               ->setBody('echo \'Hello world!\';');
         
        $class = new Zend_CodeGenerator_Php_Class();
        $class->setName('World')
              ->setMethod($method)
              ->setDocblock($docblock);
         
        $file = new Zend_CodeGenerator_Php_File();
        $file->setClass($class);
         
        // Render the generated file
        echo $file;
         
        // or write it to a file:
        file_put_contents($controller_path.'/World.php', $file->generate());
	}
}